package org.monitor.servlet.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.monitor.servlet.base.Resource;
import org.monitor.servlet.pairs.Pairs;

import com.google.gson.Gson;

public class Controller {
	
	private HttpServletRequest reqquest;
	private HttpServletResponse response;
	private Resource resource;
	private Pairs pairs;
	
	public void renderJson(Object rst){
		Gson gson = new Gson();
		try {
			getResponse().setHeader("Content-type", "text/plain;charset=UTF-8");
			getResponse().getWriter().write(gson.toJson(rst));
		} catch (IOException e) {
		}
	}
	
	public void init(HttpServletRequest reqquest, HttpServletResponse response, Resource resource, Pairs pairs){
		this.reqquest = reqquest;
		this.response = response;
		this.resource = resource;
		this.pairs = pairs;
	}

	public HttpServletRequest getReqquest() {
		return reqquest;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public Resource getResource() {
		return resource;
	}

	public Pairs getPairs() {
		return pairs;
	}
	
	public String getPair(String key){
		return getPair(pairs, key);
	}
	

	public String getPair(Pairs pairs, String key){
		try {
			return URLDecoder.decode(pairs.getPair(key), "utf-8");
		} catch (Exception e) {
			return pairs.getPair(key);
		}
	}

}
